/**
 * Bestpay.com.cn Inc.
 * Copyright (c) 2011-2020 All Rights Reserved.
 */
package com.suxiaoxiao.Leetcode.simple;

/**
 * 整数反转
 *
 * @author zhaorongzhang
 * @version ReverseInteger.java, v 0.1 2020/1/2 12:34 下午 zhaorongzhang Exp $$
 */
public class ReverseInteger {

    public int reverse(int x) {
        String reverseString = String.valueOf(x);
        boolean negtiveFlag = false;
        char[] chars = reverseString.toCharArray();
        StringBuilder resultChars = new StringBuilder();
        for (int i = chars.length - 1; i >= 0; i--) {
            if (chars[i] == '-') {
                negtiveFlag = true;
                continue;
            }
            resultChars.append(chars[i]);
        }
        int resultNumber = 0;
        try {
            resultNumber = Integer.parseInt(resultChars.toString());
            if (negtiveFlag) {
                resultNumber = -resultNumber;
            }
        } catch (Exception e) {
            return resultNumber;
        }

        return resultNumber;
    }

}